<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Pageadver;
use Illuminate\Http\Request;

class PageadverController extends Controller
{
    public function __construct()
    {
        $this->middleware('grant:pageadver');
    }

    /**
     * 进入页面广告图片列表页面
     */
    public function getIndex()
    {
        return view('admin/pageadver/index');
    }

    /**
     * 进入添加页面
     */
    public function getAdd()
    {
        $token = session('token');
        if ($token['isHidden'] == false) {
            return $this->warning('无此操作权限！');
        }
        return view('admin/pageadver/add');
    }

    /**
     * 进入编辑页面
     */
    public function getEdit(Request $request)
    {
        $id = intval($request->input('id'));
        $row = Pageadver::find($id);
        if (empty($row)) {
            return $this->warning('无效的记录');
        }
        return view('admin/pageadver/edit', $row);
    }

    /**
     * 获取分页数据
     */
    public function getPageRows(Request $request)
    {
        $kw = strval($request->input('kw'));
        $page = toLimitLng($request->input('page'), 1);
        $limit = toLimitLng($request->input('limit'), 1);
        $offset = ($page - 1) * $limit;

        $query = Pageadver::where('title', 'LIKE', '%' . $kw . '%');
        $total_rows = $query->count();
        $rows = $query->orderBy('id', 'ASC')->skip($offset)->take($limit)->get();
        foreach ($rows as $row) {
            $row->pic1 = $row->pic1 ? asset($row->pic1) : '';
        }
        $data['rows'] = $rows;

        $data['total_rows'] = $total_rows;
        $data['page_count'] = ceil($total_rows / $limit);
        $data['page'] = $page;
        return $this->response($data);
    }

    /**
     * 新增记录
     */
    public function postInsert(Request $request)
    {
        $token = session('token');
        if ($token['isHidden'] == false) {
            return $this->error('Access denied.');
        }
        $row = new Pageadver;
        $row->title = safe($request->input('title'));
        $row->url = safe($request->input('url'), 200);
        $row->width = intval($request->input('width'));
        $row->height = intval($request->input('height'));
        $row->status = toLimitLng($request->input('status'), 0, 1);
        $row->save();
        return $this->response($row);
    }

    /**
     * 修改记录
     */
    public function postUpdate(Request $request)
    {
        $id = intval($request->input('id'));
        $row = Pageadver::find($id);
        if (empty($row)) {
            return $this->error('无效的记录');
        }
        if ($request->exists('title')) {
            $row->title = safe($request->input('title'));
        }
        if ($request->exists('url')) {
            $row->url = safe($request->input('url'), 200);
        }
        if ($request->exists('width')) {
            $row->width = intval($request->input('width'));
        }
        if ($request->exists('height')) {
            $row->height = intval($request->input('height'));
        }
        if ($request->exists('status')) {
            $row->status = toLimitLng($request->input('status'), 0, 1);
        }
        if ($row->save()) {
            return $this->response($row);
        }
        return $this->error('操作失败');
    }

    /**
     * 上传图片
     */
    public function postUploadPic1(Request $request)
    {
        $id = intval($request->input('id'));
        $row = Pageadver::find($id);
        if (empty($row)) {
            return $this->error('无效的记录');
        }

        $imagedata = file_get_contents('php://input');
        $target_path = 'upload/' . date('Y/m/d/');
        $targetPath = public_path() . DIRECTORY_SEPARATOR . $target_path;
        if (!is_dir($targetPath)) {
            if (!mkdir($targetPath, 0777, 1)) {
                return $this->error('无法建立上传目录');
            }
        }
        $new_filename = uniqid(mt_rand()) . '.jpg';
        $targetFile = $targetPath . $new_filename;
        if (file_put_contents($targetFile, $imagedata)) {
            file_exists($row->pic1) and unlink($row->pic1);
            $row->pic1 = $target_path . $new_filename;
            $row->save();
            $row->pic1 = asset($row->pic1);
            return $this->response($row);
        }
        return $this->response('操作失败');
    }

    /**
     * 删除记录
     */
    public function postDelete(Request $request)
    {
        $id = intval($request->input('id'));
        $row = Pageadver::find($id);
        if (empty($row)) {
            return $this->error('无效的记录');
        }
        file_exists($row->pic1) and unlink($row->pic1);
        $row->delete();
        return $this->response($id);
    }
}
